class Solution
{
public:
    int equalPairs(vector<vector<int>> &grid)
    {
        map<vector<int>, int> rowCount;
        for (auto &row : grid)
        {
            ++rowCount[row];
        }
        int n = grid.size();
        int result = 0;
        for (int i = 0; i < n; ++i)
        {
            vector<int> col(n);
            for (int j = 0; j < n; ++j)
            {
                col[j] = grid[j][i];
            }
            result += rowCount[col];
        }
        return result;
    }
};